package com.nlx.notes.module.controller.gold;

import com.nlx.notes.module.bean.dto.gold.*;
import com.nlx.notes.module.bean.vo.DayFlowViewVo;
import com.nlx.notes.module.bean.vo.PageResultVo;
import com.nlx.notes.module.bean.vo.gold.*;
import com.nlx.notes.module.core.util.JSONUtils;
import com.nlx.notes.module.service.gold.GoldManageService;
import com.nlx.notes.module.service.gold.IGoldPurchaseService;
import com.nlx.notes.support.context.UserHolder;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping(value = "/gold/m")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class GoldManageController  {

    final GoldManageService goldManageService;

    /**
     * 保存购买的黄金信息
     * @param goldPurchaseAddDTO 购买参数
     * @return GoldPurchaseAddVO
     */
    @PostMapping(value = "save/v1")
    public GoldPurchaseBaseInfoVO saveGold(@RequestBody @Validated GoldPurchaseAddDTO goldPurchaseAddDTO){
        log.info("save gold purchase,params ->{}", JSONUtils.toString(goldPurchaseAddDTO));
        goldPurchaseAddDTO.setUserId(UserHolder.getUserContext().getId());
        return goldManageService.saveGold(goldPurchaseAddDTO);
    }

    /**
     * 修改黄金信息
     * @param goldPurchaseEditDTO 黄金参数
     * @return GoldPurchaseBaseInfoVO
     */
    @PostMapping(value = "/v1/edit")
    public GoldPurchaseEditVo editGold(@RequestBody @Validated GoldPurchaseEditDTO goldPurchaseEditDTO){
        log.info("edit gold purchase,params ->{}", JSONUtils.toString(goldPurchaseEditDTO));
        goldPurchaseEditDTO.setUserId(UserHolder.getUserContext().getId());
        return goldManageService.editGold(goldPurchaseEditDTO);
    }

    @PostMapping(value = "/v1/delete/{id}")
    public boolean deleteGold(@PathVariable("id") Long id){
        log.info("delete gold purchase,params ->{}", JSONUtils.toString(id));
        return goldManageService.deleteGold(id,UserHolder.getUserContext().getId());
    }



    /**
     * 查询黄金详情
     * @param goldId 黄金id
     * @return
     */
    @GetMapping(value = "/v1/detail")
    public GoldPurchaseEditVo detail(@RequestParam(value = "goldId")Long goldId){
        Long userId = UserHolder.getUserContext().getId();
        log.info("查询黄金详细信息，黄金id：{},用户id：{}",goldId,userId);
        return goldManageService.detail(goldId,userId);
    }


    @GetMapping(value = "/page/v1")
    public PageResultVo<GoldPurchasePageVO> page(GoldPurchasePageDTO goldPurchasePageDTO){
        goldPurchasePageDTO.setUserId(UserHolder.getUserContext().getId());
        log.info("page gold purchase,params ->{}", JSONUtils.toString(goldPurchasePageDTO));
        return goldManageService.goldPage(goldPurchasePageDTO);
    }

    @GetMapping(value = "/v1/overview")
    public GoldOverviewVO overview(){
        Long userId = UserHolder.getUserContext().getId();
        log.info("query gold overview,userId -> {}", userId);
        return goldManageService.overview(userId);

    }


    /**
     * 黄金售卖概览
     * @return GoldSaleOverviewVO
     */
    @GetMapping(value = "/v1/sale/overview")
    public GoldSaleOverviewVO saleOverview(){
        Long userId = UserHolder.getUserContext().getId();
        return goldManageService.findGoldSaleOverview(userId);
    }


    /**
     * 黄金售卖列表
     * @return GoldSalePageVO
     */
    @GetMapping(value = "/v1/gold-sale-page")
    public PageResultVo<GoldSalePageVO> goldSalePage(GoldSalePageQueryDTO goldSalePageQueryDTO){
        Long userId = UserHolder.getUserContext().getId();
        goldSalePageQueryDTO.setUserId(userId);
        return goldManageService.goldSalePage(goldSalePageQueryDTO);
    }

    /**
     * 卖出黄金
     * @param goldSaleAddDTO 卖出参数
     * @return boolean
     */
    @PostMapping(value = "/v1/sale")
    public boolean saleGold(@RequestBody GoldSaleAddDTO goldSaleAddDTO){
        goldSaleAddDTO.setUserId(UserHolder.getUserContext().getId());
        log.info("sale gold ,params ->{}", JSONUtils.toString(goldSaleAddDTO));
        return goldManageService.saleGold(goldSaleAddDTO);
    }

}
